import { gg } from "../../frame/gg";
import { Control_Listen } from "../../constants/ui_def";


const { ccclass, property } = cc._decorator;

@ccclass
export default class transition extends cc.Component {

    @property(cc.Node)              //过度背景node
    loadIngBg: cc.Node = null


    to_scene: string = null         //准备跳转的场景名字
    last_ani: string = null         //上个动画状态名字
    bgTween: cc.Tween = null            //tween,唯一

    initPage() { }
    updatePage(param) {
        this.initInfo(param)
        console.log(param)
        cc.director.on('transition', this.listenClient, this)
    }
    initInfo(param) {
        // 三个状态
        // 1.载入态 开始Load 开启关闭监听
        // 2.关闭态 监听载入态关闭后触发 触发玩后关闭整个页面
        // 3.进入态 触发载入下一个场景函数，如果当前场景状态相同。则开启关闭监听
        let transition_type = Control_Listen.LOAD_TRANSITION
        if (param && param.type) {
            transition_type = param.type
        }
        switch (transition_type) {
            case Control_Listen.CLOSE_TRANSITION:
                this.changeAni('open')
                break
            case Control_Listen.LOAD_TRANSITION:
                this.changeAni('load')
                break
            case Control_Listen.OPEN_TRANSITION:
                this.to_scene = param.scene
                this.changeAni('close')
                break
        }
    }
    // 切换场景
    toScene(scene) {
        if (!scene) {
            cc.error("跳尼玛呢，场景值都没输入")
            return
        }
        if (gg.uiManager.lastScene != scene) {
            cc.director.loadScene(scene);
        }
    }
    closeUI() {
        cc.director.off('transition', this.listenClient, this)
    }
    changeAni(ani) {
        if (this.bgTween) {
            this.bgTween.stop()
            this.bgTween = null
        }
        if (this.last_ani == ani) {
            return
        }
        switch (ani) {
            case 'load':
                this.bgTween = cc.tween(this.loadIngBg)
                    .to(0.3, { opacity: 255 })
                    .start()
                break
            case 'open':
                this.bgTween = cc.tween(this.loadIngBg)
                    .to(0.3, { opacity: 0 })
                    .start()
                break
            case 'close':
                this.loadIngBg.opacity = 0
                this.bgTween = cc.tween(this.loadIngBg)
                    .to(1, { opacity: 255 })
                    .call(() => {
                        this.toScene(this.to_scene)
                    })
                    .start()
        }
        this.last_ani = ani
    }
    // 关闭监听
    listenClient(code, param, arg) {
        if (code) {
            switch (code) {
                case Control_Listen.CLOSE_TRANSITION:
                    this.changeAni("open")
                    break
            }
        }
    }
}
